{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5943d80d",
   "metadata": {},
   "source": [
    "### 集合的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5da7b76a",
   "metadata": {},
   "source": [
    "#### 1.定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24889846",
   "metadata": {},
   "source": [
    "set 和 dict一样，只是没有 value，相当于 dict 的 key 集合。由于 dict 中的 key 不能重复，所以，在 set 中没有重复的元素。故集合（set）是一个无序的不重复元素序列。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6eeaff6",
   "metadata": {},
   "source": [
    "2. 集合的格式  \n",
    "* 集合名 = {元素1 , 元素2 , 元素3 , ... ...}  \n",
    "* 集合名 = set( 元素 )  \n",
    "3. 集合的特点。\n",
    "* 创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典；  \n",
    "* set 里边的元素是不可重复的，所以集合可用于元素去重（如：列表去重）；\n",
    "* et 是无序的，支持并、交、差及对称差等数学运算， 但由于 set 不记录元素位置，因此不支持索引、分片等类序列的操作。  \n",
    "* set 不能嵌套，不能存可以改变的数据类型，如列表，字典；可以存数字、字符串、元组；  \n",
    "* set 和 dict 的唯一区别仅在于没有存储对应的 value，但是，set 的原理和 dict 一样，所以，同样不可以放入可变对象，因为无法判断两个可变对象是否相等，也就无法保证 set 内部“不会有重复元素”。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50bc16ac",
   "metadata": {},
   "source": [
    "### 二.集合的基本操作  \n",
    "1. 创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6ca6763d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set0: set() <class 'set'>\n",
      "set1: {1, 2, 3, 4} <class 'set'>\n",
      "set2: {1, 2, 3, 4} <class 'set'>\n",
      "set3: {'d', 'r', 'H', 'W', 'e', '!', 'o', 'l'} <class 'set'>\n",
      "set4: {'weigh', 'age', 'name'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#创建一个空集合时，只能使用s=set()，因为s={}创建的是空字典\n",
    "set0 = set()  \n",
    "#创建一个数值集合，由此我们也可以看出集合可自动去重\n",
    "set1 = set([1,2,3,4])                                 #故set不能存列表，会将其自动转化为set\n",
    "set2 = {1,2,3,4}\n",
    "#创建一个字符集合\n",
    "set3= set(\"HelloWorld!\")\n",
    "#set和dict一样，只是没有value，相当于dict的key集合。\n",
    "set4 = set({'name':'zhangsan','age':20,'weigh':50})   #故set不能存字典，会将其自动转化为set\n",
    " \n",
    "print('set0:',set0,type(set0))\n",
    "print('set1:',set1,type(set1))\n",
    "print('set2:',set2,type(set2))\n",
    "print('set3:',set3,type(set3))\n",
    "print('set4:',set4,type(set4))\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a74db1",
   "metadata": {},
   "source": [
    "2、去重\n",
    "\n",
    "也可实现去除掉列表中的重复元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b581a1f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set1去重后： {1, 2, 3, 4, 5}\n",
      "list1去重后： [3, 4, 5, 6, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#集合去重\n",
    "set1={1,2,3,4,5,2}\n",
    "print(\"set1去重后：\",set1)\n",
    "#列表去重\n",
    "list1 = [3,6,9,4,5,3,5,6,7]\n",
    "new_list = list(set(list1))\n",
    "print(\"list1去重后：\",new_list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba164414",
   "metadata": {},
   "source": [
    "3、集合元素的增、删以及判断元素是否在集合中存在\n",
    "\n",
    "set 是无序的，  不记录元素的位置，因此不支持索引、分片等类序列的操作。\n",
    "3.1> 增加元素\n",
    "set.add(x)：将元素 x 添加到集合 set 中，如果元素已存在，则不进行任何操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a7b792f",
   "metadata": {},
   "source": [
    "3.1> 增加元素\n",
    "set.add(x)：将元素 x 添加到集合 set 中，如果元素已存在，则不进行任何操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d06cf037",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Taobao', 'Facebook', 'Google', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"Google\", \"Runoob\", \"Taobao\"}\n",
    "set1.add(\"Facebook\")\n",
    "print(set1)\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70f52cc7",
   "metadata": {},
   "source": [
    "set.update(x)：参数可以是列表，元组，字典等；x 可以有多个，用逗号分开。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c491c24e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 9, 'Google', 'Taobao', 'Runoob'}\n",
      "{0, 1, 3, 9, 'Google', 'Taobao', 'Runoob'}\n",
      "{0, 1, 3, 4, 5, 6, 9, 'Google', 'Taobao', 'Runoob'}\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"Google\", \"Runoob\", \"Taobao\"}\n",
    "set1.update((9,0))\n",
    "print(set1)\n",
    "set1.update({1,3})\n",
    "print(set1)\n",
    "set1.update([1,4],[5,6])  \n",
    "print(set1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0540e5df",
   "metadata": {},
   "source": [
    "3.2> 删除元素\n",
    "set.remove(x)：将元素 x 从集合 s 中移除，如果元素不存在，则会发生错误。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a529aa53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Google', 'Runoob'}\n",
      "-------------------------------\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'Facebook'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-9555f1dc71d0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mset1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'-------------------------------'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mset1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Facebook\"\u001b[0m\u001b[0;34m)\u001b[0m   \u001b[0;31m# 不存在会发生错误\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'Facebook'"
     ]
    }
   ],
   "source": [
    "set1 = {\"Google\", \"Runoob\", \"Taobao\"}\n",
    "set1.remove(\"Taobao\")\n",
    "print(set1)\n",
    "print('-------------------------------')\n",
    "set1.remove(\"Facebook\")   # 不存在会发生错误\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1951b7fe",
   "metadata": {},
   "source": [
    "4、判断元素是否在集合中存在\n",
    "x in set：判断元素 x 是否在集合 s 中，存在返回 True，不存在返回 False。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "17d09b53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "set1 = {\"Google\", \"Runoob\", \"Taobao\"}\n",
    "print(\"Runoob\" in set1)\n",
    "print(\"Facebook\" in set1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a380400",
   "metadata": {},
   "source": [
    "### 三、集合的运算操作\n",
    "\n",
    "即集合的最基本操作，如集合取交集(&)、并集(|)、取差集、判断一个集合是不是另一个集合的子集或者父集等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "245f790b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a={1,3,4,2,5}\n",
    "b={1,2,3,6,7}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66f4c549",
   "metadata": {},
   "source": [
    "1. 取交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8f561dcf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3}\n",
      "{1, 2, 3}\n"
     ]
    }
   ],
   "source": [
    "set1 = {1,3,4,2,5}\n",
    "set2 = {1,2,3,6,7}\n",
    "a = set1 & set2\n",
    "print(a)\n",
    "b = set1.intersection(set2)\n",
    "print(b)\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad6f8305",
   "metadata": {},
   "source": [
    "2. 取并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5ef993d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7}\n",
      "{1, 2, 3, 4, 5, 6, 7}\n"
     ]
    }
   ],
   "source": [
    "set1 = {1,3,4,2,5}\n",
    "set2 = {1,2,3,6,7}\n",
    "a = set1 | set2\n",
    "print(a)\n",
    "b = set1.union(set2)\n",
    "print(b)\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35974cad",
   "metadata": {},
   "source": [
    "3. 差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "47a843c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{4, 5}\n",
      "{4, 5}\n"
     ]
    }
   ],
   "source": [
    "set1 = {1,3,4,2,5}\n",
    "set2 = {1,2,3,6,7}\n",
    "a = set1 - set2      #set1减去set1与set2的交集\n",
    "print(a)  \n",
    "b=set1.difference(set2)\n",
    "print(b)\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86dd975a",
   "metadata": {},
   "source": [
    "4. 子集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d34b89ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "set1 = {1,2,3,4,5}\n",
    "set2 = {1,3,5}\n",
    "a =set2.issubset(set1)   #set2是set1的子集\n",
    "print(a)\n",
    "b=set1.issuperset(set2)  #set1是set2的父集\n",
    "print(b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd3143f4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
